<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
 // Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}
function loadXMLDoc()
{
var xmlhttp = GetXmlHttpObject();

//当使用 async=true 时，请规定在响应处于 onreadystatechange 事件中的就绪状态时执行的函数：
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
  }
/*
 * get 请求
	为了避免得到的是缓存的结果这种情况，请向 URL 添加一个唯一的 ID：Math.random()
 */
//xmlhttp.open("GET","ajax.php?t=" + Math.random() + "&name=heaven",true);
//xmlhttp.open("GET","file.txt",true);
//xmlhttp.send();
/*
 * post 请求
 */
xmlhttp.open("POST","ajax.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("name=heaven&t="+ Math.random());
}
/*
 * XML响应
 */
function loadXMLDoc2()
{
var txt,x,i;
var xmlhttp = GetXmlHttpObject();
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    xmlDoc=xmlhttp.responseXML;
    txt="";
    x=xmlDoc.getElementsByTagName("year");
    for (i=0;i<x.length;i++)
      {
      txt=txt + x[i].childNodes[0].nodeValue + "<br />";
      }
    document.getElementById("myDiv").innerHTML=txt;
    }
  }
xmlhttp.open("GET","ajax.xml",true);
xmlhttp.send();
}

/*
 * 使用 Callback 函数
 */
 var xmlhttp;
 function loadXMLDocc(url,cfunc)
 {
 xmlhttp = GetXmlHttpObject();
 xmlhttp.onreadystatechange=cfunc;
 xmlhttp.open("POST",url,true);
 xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
 xmlhttp.send("t="+Math.random()+"&name=heaveddn");
 }
 function myFunction()
 {
 loadXMLDocc("ajax.php",function()
   {
   if (xmlhttp.readyState==4 && xmlhttp.status==200)
     {
     document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
     }
   });
 }
</script>
</head>
<body>
<h2>Let AJAX change this text</h2>
<button type="button" onclick="loadXMLDoc()">通过 AJAX 改变内容</button>
<button type="button" onclick="loadXMLDoc2()">XML响应</button>
<button type="button" onclick="myFunction()">使用 Callback 函数</button>
<div id="myDiv"></div>
</body>
</html>